<template>
  <div class="mod-menu">
    <el-form :inline="true" :model="pamars">
      <el-form-item label="服务名称 ">
        <el-input v-model="pamars.title" placeholder="请输入服务名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getListOne()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" row-key="Id" border style="width: 100%; ">
      <el-table-column prop="title" align="center" label="服务名称">
      </el-table-column>
      <el-table-column prop="sale_price" align="center" label="价格">
      </el-table-column>
      <el-table-column prop="real_sale_num" align="center" label="实际销量">
      </el-table-column>
      <el-table-column prop="sort" align="center" label="排序">
      </el-table-column>
      <el-table-column prop="auditStatus" align="center" :formatter="statusFormatter" label="审核状态">
      </el-table-column>
      <el-table-column prop="verify_remark" align="center" label="审核详情" width="200px" show-overflow-tooltip>
        <template slot-scope="scope">
          {{scope.row.verify_remark || ''}}
        </template>
      </el-table-column>
      <el-table-column prop="added" align="center" label="上下架">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.added" :active-value="true" :inactive-value="false" @change="changeSwitch(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column prop="create_time" align="center" label="创建时间"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button v-if="false" type="text" size="small" @click="goShareBenefit(scope.row.id)">分润设置</el-button>
          <el-button type="text" size="small" @click="handleClickDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pamars.limit" :page-sizes="[10, 20, 50, 100]" :page-size="pamars.page" :total="totalPage" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <serve-add-update v-if="setShareBenefit" ref="serveAddUpdate"></serve-add-update>
    <!--商品分佣设置 -->
    <pd-shareProfit v-if="setShareBenefit" @result="settingResult" ref="pdShareProfit" :type='2'></pd-shareProfit>
  </div>
</template>

<script>
import pdShareProfit from './pd-shareProfit.vue'
import serveAddUpdate from './serve-add-update'
import seviceApi from '@/api/serviceApi.js'
import serviceApione from '@/api/serviceApione.js'
export default {
  data () {
    return {
      dataForm: {},
      tableData: [],
      dataListLoading: false,
      addOrUpdateVisible: false,
      setShareBenefit: false, // 分润设置
      pamars: {
        page: 1,
        limit: 10
      },
      totalPage: 0
    }
  },
  components: {
    serveAddUpdate,
    pdShareProfit
  },
  mounted () {
    this.getList()
  },
  methods: {
    settingResult () {
      this.getList()
      this.setShareBenefit = false
    },
    // 分润设置
    goShareBenefit (id) {
      this.setShareBenefit = true
      this.$nextTick(() => {
        this.$refs.pdShareProfit.init(id)
      })
    },
    handleClickDelete (row) {
      this.$confirm(`是否删除服务 [ ${row.title} ] ?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        serviceApione.delServiceGoods(row.id).then(res => {
          console.log(res)
          if (res.data && res.data.code == 200) {
            this.$message.success('删除成功！')
            this.getList()
          }
        })
      }).catch(() => { })
    },
    changeSwitch (row) {
      let msg = ''
      let param = {
        goods_id: row.id,
        added: row.added
      }
      if (row.added) {
        msg = '已上架'
      } else {
        msg = '已下架'
      }
      serviceApione.servicePutAway(param).then(res => {
        if (res.data && res.data.code == 200) {
          this.$message.success(msg)
        } else {
          row.added = !row.added
        }
      })
    },
    getList () {
      seviceApi.getServiceGoodsList(this.pamars).then(res => {
        if (res && res.data.code == 200) {
          this.tableData = res.data.page.list
          this.totalPage = res.data.page.totalCount
        }
      })
    },
    getListOne () {
      this.pamars.page = 1
      this.getList()
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pamars.limit = val
      this.getList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pamars.page = val
      this.getList()
    },
    // 审核动态
    statusFormatter (row, column) {
      let status = row.verify_status
      if (status === 0) {
        return '待审核'
      } else if (status === 1) {
        return '审核通过'
      } else if (status === 2) {
        return '审核不通过'
      } else {
        return '-'
      }
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.$router.push({
        path: '/send-service',
        query: {
          id: id
        }
      })
      // this.setShareBenefit = true
      // this.$nextTick(() => {
      //   this.$refs.serveAddUpdate.init(id)
      // })
    }
  }
}
</script>
